<template>
  <div class="jobDetail-container" v-loading="loading">
    <div class="job-detail-header">
      <div
        style="
          height: 100%;
          display: flex;
          flex-direction: column;
          justify-content: center;
        "
      >
        <div style="display: flex; align-items: center">
          <span style="font-size: 24px; font-weight: bold">
            {{ jobDetail?.name }}({{ jobDetail?.location }})
          </span>
          <el-tag type="success" style="margin-left: 12px">
            {{ handleJobStatus(jobDetail?.status ? jobDetail.status : 0) }}
          </el-tag>
          <el-tag type="success" style="margin-left: 12px">
            {{ handleJobStatus(jobDetail?.status ? jobDetail.status : 0) }}
          </el-tag>
          <el-text type="danger" style="margin-left: 12px; font-size: 24px">
            {{ jobDetail?.minSalary }}-{{ jobDetail?.maxSalary }}K
          </el-text>
        </div>
        <div style="padding: 16px 0">
          <el-tag style="margin-right: 8px; font-size: 14px">
            {{ jobDetail?.tagName }}
          </el-tag>
          <el-tag style="margin-right: 8px; font-size: 14px">
            {{
              handleEducationEnum(jobDetail?.eduction ? jobDetail.eduction : 0)
            }}
          </el-tag>
          <el-tag style="margin-right: 8px; font-size: 14px">
            {{
              handleExperienceEnum(
                jobDetail?.experience ? jobDetail.experience : 0,
              )
            }}
          </el-tag>
          <el-tag style="margin-right: 8px; font-size: 14px">
            {{ handleJobType(jobDetail?.type ? jobDetail.type : 0) }}
          </el-tag>
        </div>
        <div v-if="jobDetail?.otherTag" style="padding: 0 8px">
          <el-tag
            type="info"
            style="margin-right: 8px"
            v-for="item in jobDetail?.otherTag"
          >
            {{ item }}
          </el-tag>
        </div>
      </div>
      <div>
        <el-button
          :disabled="judgeHr()"
          type="primary"
          size="large"
          round
          @click="addProcess()"
        >
          投递简历
        </el-button>
        <el-button
          :disabled="judgeHr()"
          type="warning"
          size="large"
          round
          @click="gotoChat()"
        >
          聊一聊
        </el-button>
      </div>
    </div>
    <div class="job-detail-hr-info">
      <el-card style="width: 100%" shadow="always">
        <div
          style="
            display: flex;
            align-items: center;
            justify-content: space-between;
          "
        >
          <div
            style="display: flex; align-items: center; cursor: pointer"
            @click="
              $router.push('/info/company/' + jobDetail?.companyInfoVo.id)
            "
          >
            <el-avatar :src="jobDetail?.jorHrInfoVo.avatar" :size="54" />
            <div style="margin-left: 12px">
              <p style="padding: 4px 0; font-size: 22px">
                {{ jobDetail?.jorHrInfoVo.realname }}
              </p>
              <el-text type="info">{{ jobDetail?.jorHrInfoVo.job }}</el-text>
              <el-text type="info">
                · {{ jobDetail?.companyInfoVo.name }}
              </el-text>
            </div>
          </div>
          <el-button
            type="warning"
            size="large"
            :disabled="judgeHr()"
            round
            @click="gotoChat()"
          >
            聊一聊
          </el-button>
        </div>
      </el-card>
    </div>
    <div class="job-detail-description">
      <p style="font-size: 18px; font-weight: bold">职位描述：</p>
      <v-md-preview :text="jobDetail?.description"></v-md-preview>
    </div>
    <div class="job-detail-company-info">
      <el-card
        style="width: 100%; cursor: pointer"
        shadow="always"
        @click="$router.push('/info/company/' + jobDetail?.companyInfoVo.id)"
      >
        <div style="display: flex; align-items: center">
          <el-avatar :src="jobDetail?.companyInfoVo.logo" :size="54" />
          <div style="margin-left: 12px">
            <p style="padding: 4px 0; font-size: 20px">
              {{ jobDetail?.companyInfoVo.name }}
            </p>
            <el-tag type="info">
              {{ jobDetail?.companyInfoVo.location }}
            </el-tag>
            <el-tag type="info" style="margin-right: 8px">
              {{
                handleFinancingStage(
                  jobDetail?.companyInfoVo.financingStage
                    ? jobDetail?.companyInfoVo.financingStage
                    : 0,
                )
              }}
            </el-tag>
            <el-tag type="info" style="margin-right: 8px">
              {{
                handleScala(
                  jobDetail?.companyInfoVo.scala
                    ? jobDetail?.companyInfoVo.scala
                    : 0,
                )
              }}
            </el-tag>
            <el-tag
              type="info"
              style="margin-right: 8px"
              v-for="one in jobDetail?.companyInfoVo.otherTag"
            >
              {{ one }}
            </el-tag>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reqGetJobInfoById } from '@/api/job'
import { JobType } from '@/api/job/type'
import {
  handleEducationEnum,
  handleExperienceEnum,
  handleJobStatus,
  handleJobType,
} from '@/constant/type'
import { ElMessage } from 'element-plus'
import { computed, onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { handleFinancingStage, handleScala } from '@/constant/type'
import { reqAddProcess } from '@/api/recruitProcess'
import useUserStore from '@/store/modules/user'

onMounted(() => {
  getJobDetailById(currentJobId.value)
  judgeHr()
})

const jobDetail = ref<JobType>()
const loading = ref(false)
const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

const judgeHr = () => {
  if (userStore.isHr === 1) {
    console.log('hr身份')
    return true
  }
  console.log('求职者身份')
  return false
}

const currentJobId = computed(() => {
  if (route.params.id) {
    return parseInt(route.params.id as string)
  } else {
    return 0
  }
})

const getJobDetailById = async (id: number) => {
  try {
    loading.value = true
    const { data } = await reqGetJobInfoById(id)
    jobDetail.value = data.data
  } catch (e) {
    ElMessage.error((e as Error).message)
  } finally {
    loading.value = false
  }
}

const addProcess = async () => {
  try {
    const { data } = await reqAddProcess({ jobId: currentJobId.value })
    ElMessage.success(data.message)
  } catch (e) {
    ElMessage.error((e as Error).message)
  }
}

const gotoChat = () => {
  sessionStorage.setItem('jobInfo', JSON.stringify(jobDetail.value))
  router.push({
    path: '/chat',
    query: {
      jId: jobDetail.value?.id,
    },
  })
}
</script>

<style scoped lang="scss">
.jobDetail-container {
  max-width: 100vw;
  min-width: 1200px;
  height: 100%;

  .job-detail-header {
    display: flex;
    justify-content: space-between;
    padding: 48px 15% 0;
  }

  .job-detail-hr-info {
    width: 100%;
    padding: 24px 15% 0;
  }

  .job-detail-description {
    width: 100%;
    padding: 24px 15% 0;
  }

  .job-detail-company-info {
    width: 100%;
    padding: 16px 15% 0;
  }
}
</style>
